var table1;
$(function () {
    initTime();
    layui.use('table', function () {
        var table = layui.table;
        table1 = table.render({
            elem: '#accountManageTable',
            method: 'post',
            toolbar: '#toolbarDemo',
            contentType: "application/json",
            url: ctx + "/user/queryByPage",
            title: '账号列表',
            page: true,
            request: {
                pageName: 'currentPage',
                limitName: 'pageSize'
            },
            limit: 10,
            limits: [10, 15, 20, 25],
            height: 'full-120',
            cols: [
                [
                    {
                        title: '序号', width: 50, templet: function (obj) {
                            return obj.LAY_INDEX
                        }
                    },
                    {field: 'userName', align: 'center', width: '150', title: '用户名'},
                    {field: 'password', align: 'center', width: '150', title: '密码',},
                    {
                        field: 'role', align: 'center', width: '100', title: '角色',
                        templet: function (d) {
                            return d.role == 0 ? '<span style="color: #cc1800">' + '超级管理员' + '</span>' :
                                '<span style="color: rgb(37,204,0);">' + '小区管理员' + '</span>'
                        }
                    },
                    {field: 'realName', align: 'center', width: '100', title: '真实姓名'},
                    {field: 'phone', align: 'center', width: '130', title: '手机号'},
                    {field: 'communityName', align: 'center', width: '180', title: '所属小区'},
                    {field: 'createTime', align: 'center', width: '180', title: '创建时间'},
                    {fixed: 'right', title: '操作', width: '220', align: 'center', toolbar: '#barDemo'}
                ]
            ]
        });

        //头工具栏事件
        table.on('toolbar(accountManageFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            if (obj.event == 'addNew') {
                layer.open({
                    type: 1,
                    title: '新增账号',
                    shadeClose: true,
                    shade: 0.1,
                    area: ['600px', '410px'],
                    content: $("#addNewDiv")
                })
            }
        });

        //监听行工具事件
        table.on('tool(accountManageFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'boundWx') {
                boundWx(data);
            }else if (obj.event === 'editAccountManage') {
                editAccountManage(data)
            }else if (obj.event === 'delAccountManage') {
                delAccountManage(data);
            }
        });
    });
})

//初始化时间
function initTime() {
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#createTime', //指定元素
            format: 'yyyy-MM-dd HH:mm:ss' //可任意组合
        });
    });
}

//查询
function query() {
    const condition = {name: $("#name").val()}
    table1.reload({
        where: {
            "condition": condition
        }
    });
}

//清空
function clean() {
    $("#name").val('');
    query();
    form.render();
}

//绑定微信用户
function boundWx(data) {
    layer.open({
        type: 2,
        title: "绑定微信用户（请输入全名搜索 根据手机号和头像区分 最右边有绑定按钮）",
        shadeClose: true,
        shade: 0.1,
        content: ctx + "/user/bindToWxPage" + "?id=" + data.id,
        area: ['1100px', '600px']
    })
}

//编辑接口信息
function bindWx(data) {
    layer.open({
        type: 2,
        title: "绑定微信账号",
        shadeClose: true,
        shade: 0.1,
        content: ctx + "/user/bindWxPage" + "?id=" + data.id,
        area: ['600px', '340px']
    })
}

//编辑接口信息
function editAccountManage(data) {
    layer.open({
        type: 2,
        title: "编辑账号信息",
        shadeClose: true,
        shade: 0.1,
        content: ctx + "/user/editPage" + "?id=" + data.id,
        area: ['600px', '340px']
    })
}

//删除接口
function delAccountManage(data) {
    layer.confirm('确定要删除吗?', {icon: 3, title: '温馨提示', offset: ['10%', '40%']}, function (index) {
        var arr = [];
        arr.push(data.id);
        $.ajax({
            url: ctx + '/user/delete',
            type: "POST",
            data: JSON.stringify(arr),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (success) {
                if (success.code == 200) {
                    msg.success("删除成功！");
                    table1.reload();
                } else {
                    msg.error("删除失败！")
                }
            }
        });

        layer.close(index);
    });
}